package org.yzz.org.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.yzz.base.service.impl.BaseServiceImpl;
import org.yzz.base.utils.PageInfo;
import org.yzz.org.domain.Department;
import org.yzz.org.mapper.DepartmentMapper;
import org.yzz.org.query.DepartmentQuery;
import org.yzz.org.service.IDepartmentService;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;

@Service
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {
    @Autowired
    private DepartmentMapper mapper;

   /* @Override
    public List<Department> selectAll() {
        return mapper.selectAll();
    }

    @Override
    public Department selectById(Serializable id) {
        return mapper.selectById(id);
    }

    @Override
    @Transactional
    public void insert(Department department) {
        mapper.insert(department);
        // 处理 path
        Long id = department.getParent().getId();
        if(Objects.nonNull(id)){
            // 父级id 不为空，说明有上级，根据id 找到该对象的上级
            Department parent = mapper.selectById(id);
            // 将 上级的 path 拼上自己的 id 就得到了 自己的 path
            department.setPath(parent.getPath()+"/"+department.getId());
        }else{
            // 为空说明没有父级，则直接拼
            department.setPath("/"+department.getId());
        }
        // 处理完 path 后 在数据库中更新一下自己的数据
        mapper.update(department);
    }

    @Override
    @Transactional
    public void update(Department department) {
        // 处理 path
        Long id = department.getParent().getId();
        if(Objects.nonNull(id)){
            // 父级id 不为空，说明有上级，根据id 找到该对象的上级
            Department parent = mapper.selectById(id);
            // 将 上级的 path 拼上自己的 id 就得到了 自己的 path
            department.setPath(parent.getPath()+"/"+department.getId());
        }else{
            // 为空说明没有父级，则直接拼
            department.setPath("/"+department.getId());
        }
        // 处理完 path 后 在数据库中更新一下自己的数据
        mapper.update(department);
    }

    @Override
    @Transactional
    public void delete(Serializable id) {
        mapper.delete(id);
    }

    @Override
    public PageInfo<Department> queryPage(DepartmentQuery departmentQuery) {
        // 查询总条数,带参是为了根据参数中的条件去数据库里边查询
        Integer total =mapper.countTotal(departmentQuery);
        if(total==0)
            // 返回 pageInfo 是因为传给前端是一个空对象(里面的属性都要有初始值)的话，前端对应的字段就会是一个空数组，而若返回一个 null 前端就无法接收会报错
            return new PageInfo<Department>();
        // 查询分页数据，因为传的 departmentQuery 里面有 pageSize，所以才可以知道要查多少条数据，所以也必须要带参
        List<Department> list =mapper.queryPage(departmentQuery);

        // 封装 PageInfo
        return new PageInfo<>(total,list);
    }

    @Override
    public void batchDelete(List<Long> ids) {
        mapper.batchDelete(ids);
    }*/

    @Override
    public List<Department> deptTree() {
        return mapper.deptTree();
    }
}
